<?php
/**
* 名称：资讯模块控制器。
* 描述：资讯模块相关的控制响应程式。
*/
class Controller_News_Self extends Controller_Public_Account{
    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Listview(){
        $data=array(
            'meta'=>array_merge(
                Model_Common_OUI::Breadcrumb(
                    Model_News::OUI_Breadcrumb('list','text')
                ),
                Model_Common_OUI::Button(array(
                    'text'=>'发布资讯',
                    'link'=>Route::MakeHyperlink(':news_self::add'),
                )),
                array(
                    'list_title'=>'资讯列表'
                )
            ),
            'base'=>self::$Database->GetLimit(
                'SELECT * FROM '.self::$Database->Table('news').' ORDER BY id DESC',
                Route::GetArguments('page'),
                10
            ),
            'urlArguments'=>array(
                'class'=>'0'
            )
        );
        //~
        foreach($data['base']['Rows'] as &$value){
            $value['class_name']=Model_Common_Sort::GetClassesName('news_class',$value['class']);
        }
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/news/self/list.html');
    }

    /**
    * 描述：添加之视图程式。
    * @access protected
    * @return void
    */
    protected function Add(){
        $data=array(
            'meta'=>array_merge(
                Model_Common_OUI::Breadcrumb(
                    Model_News::OUI_Breadcrumb('list'),
                    Model_News::OUI_Breadcrumb('add','text')
                ),
                array(
                    'action'=>Route::MakeHyperlink(':news_self::insert')
                )
            ),
            'class'=>Model_Common_Sort::GetClassData('news_class')
        );
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/news/self/form.html');
    }

    /**
    * 描述：添加之响应程式。
    * @access protected
    * @return void
    */
    protected function Insert(){
        $data=Model::Post(Model_News::$FormLabel);
        //~
        self::$Database->BeginTransaction();
        if(self::$Database->Insert(self::$Database->Table('news'),$data['base'])){
            $data['base']['id']=self::$Database->lastInsertId();
            if($this->Trigger($data)){
                $isInsert=TRUE;
                self::$Database->Commit();
            }
            else{
                $isInsert=FALSE;
                self::$Database->Rollback();
            }
        }
        else{
            $isInsert=FALSE;
            self::$Database->Rollback();
        }
        //~
        $arguments=array(
            'notice'=>'资讯发布成功，3秒钟后自动返回。',
            'link'=>array(
                array('name'=>'继续发布','href'=>Route::MakeHyperlink(':news_self::add')),
                array('name'=>'前往修改','href'=>Route::MakeHyperlink(':news_self::edit','id:'.$data['base']['id'])),
                array('name'=>'返回列表','href'=>Route::MakeHyperlink(':news_self::listview'))
            ),
            'autoTurn'=>array(TRUE,3)
        );
        $this->Feedback($isInsert,$arguments,'发布失败，请稍候再试！');
    }

    /**
    * 描述：修改之视图程式。
    * @access protected
    * @return void
    */
    protected function Edit(){
        $data=array(
            'meta'=>array_merge(
                Model_Common_OUI::Breadcrumb(
                    Model_News::OUI_Breadcrumb('list'),
                    Model_News::OUI_Breadcrumb('edit','text')
                ),
                array(
                    'action'=>Route::MakeHyperlink(':news_self::update')
                )
            ),
            'base'=>self::$Database->GetRow(
                'SELECT * FROM '.self::$Database->Table('news').' WHERE id=?',
                array(Route::GetArguments('id'))
            ),
            'class'=>Model_Common_Sort::GetClassData('news_class')
        );
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/news/self/form.html');
    }

    /**
    * 描述：修改之响应程式。
    * @access protected
    * @return void
    */
    protected function Update(){
        $data=Model::Post(Model_News::$FormLabel);
        $data['base']['id']=post('id');
        $data['condition']=array(
            'sql'=>'id=?',
            'parameters'=>array($data['base']['id'])
        );
        //~
        self::$Database->BeginTransaction();
        if(self::$Database->Update(self::$Database->Table('news'),$data['base'],$data['condition'])){
            if($this->Trigger($data)){
                $isUpdate=TRUE;
                self::$Database->Commit();
            }
            else{
                $isUpdate=FALSE;
                self::$Database->Rollback();
            }
        }
        else{
            $isUpdate=FALSE;
            self::$Database->Rollback();
        }
        //~
        $arguments=array(
            'notice'=>'资讯修改成功，3秒钟后自动返回。',
            'link'=>array(
                array('name'=>'继续修改','href'=>Route::MakeHyperlink(':news_self::edit','id:'.$data['base']['id'])),
                array('name'=>'返回列表','href'=>Route::MakeHyperlink(':news_self::listview'))
            ),
            'autoTurn'=>array(TRUE,3)
        );
        $this->Feedback($isUpdate,$arguments,'发布失败，请稍候再试！');
    }

    /**
    * 描述：资讯删除程式之删除。
    * @access protected
    * @return void
    */
    protected function Delete(){
        $id=Route::GetArguments('id');
        //~
        $this->Feedback(
            self::$Database->ExecParameterized(
                'DELETE FROM '.self::$Database->Table('news').' WHERE id=?',array($id)
            ),
            $_SERVER['HTTP_REFERER'],
            '删除失败，请稍候再试！'
        );
    }

    /**
    * 描述：触发程式之添加修改之头条、要闻。
    * @access private
    * @return boolean
    */
    private function Trigger($data){
        $var=array(
            'result'=>TRUE,
            'meta'=>array(
                array('action'=>$data['meta']['headline']==='1','table'=>'news_headline'),
                array('action'=>$data['meta']['highlights']==='1','table'=>'news_highlights')
            ),
            'data'=>array(
                'title'=>$data['base']['title'],
                'link'=>Route::MakeHyperlink('0002','news::detail',array('id:'.$data['base']['id'])),
                'summary'=>$data['base']['summary']
            )
        );
        //~
        foreach($var['meta'] as $item){
            $table=self::$Database->Table($item['table']);
            if($item['action']){
                if(self::$Database->Insert($table,$var['data'])){
                    $id=self::$Database->LastInsertId();
                    $sql='UPDATE '.$table.' SET orderNumber=? WHERE id=?';
                    if(!self::$Database->ExecParameterized($sql,array($id,$id))){
                        return FALSE;
                    }
                }
                else{
                    return FALSE;
                }
            }
        }
        //~
        return $var['result'];
    }
}